package com.operate.api.operate.version.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ZipUtil;

import com.operate.api.base.R;
import com.operate.api.operate.version.service.ISysVersionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;


@Service
@Transactional
@Slf4j
public class SysVersionServiceImpl implements ISysVersionService {


    @Override
    public R updateWeb(MultipartFile file) throws IOException {
        String path= "/data/ui/new/operate";
//        String path= System.getProperty("user.dir")+"/ui";
//        String zipPath= System.getProperty("user.dir")+"/uiZip";
        String zipPath= "/data/ui/new/operateZip";
        String timeDirectory = DateUtil.format(new Date(), "yyyyMMddHHmmss");
        File dest = new File(zipPath+"/"+timeDirectory+file.getOriginalFilename());
        if (!dest.getParentFile().exists()) {// 检测是否存在目录
            dest.getParentFile().mkdirs();
        }
        file.transferTo(dest);
//        ZipUtil.zip(path, zipPath+"/old"+timeDirectory+file.getOriginalFilename());
        FileUtil.del(path);
        ZipUtil.unzip(zipPath+"/"+timeDirectory+file.getOriginalFilename(),path);
        return new R();
    }
}